/*
 * SessionsManagerBean.java
 *
 * Created on 23 wrzesień 2007, 14:20
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package info.mikrotubula.managers;

import info.mikrotubula.db.Sessions;
import info.mikrotubula.db.Users;
import java.util.Calendar;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.sql.DataSource;

/**
 *
 * @author Krystian
 */
@Stateless
public class SessionsManagerBean implements SessionsManagerLocal {

    @EJB
    private UsersManagerLocal usersManagerBean;
    
    @PersistenceContext(unitName = "persistence")
    protected EntityManager em;    
    
    /** Creates a new instance of SessionsManagerBean */
    public SessionsManagerBean() {
    }
    
    public Sessions create() {
        Sessions session = new Sessions();
        em.persist(session);
        return session;
    }
    
    public Sessions authenticate(Sessions session, String userName, String userPass) {
        Users user = usersManagerBean.findByNameAndPass(userName, userPass);
        if (user == null) return session;
        session.setSesAuth(true);
        session.setSesDate(Calendar.getInstance().getTime());
        session.setUser(user);
        em.merge(session);
        return session;
    }
    
    public void delete(Sessions session) {
        em.remove(session);
    }
}
